package io.renren.modules.yckj.controller;

import io.renren.modules.security.user.SecurityUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.renren.common.annotation.LogOperation;

import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.yckj.dto.CateDTO;
import io.renren.modules.yckj.excel.CateExcel;
import io.renren.modules.yckj.service.CateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 项目分类表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2025-04-22
 */
@RestController
@RequestMapping("yckj/cate")
@Tag(name = "项目分类表")
public class CateController {
    @Autowired
    private CateService cateService;

//    @GetMapping("page")
//    @Operation(summary = "分页")
//      @Parameters({
//        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
//        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
//        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
//        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
//    })
//    @RequiresPermissions("yckj:cate:page")
//    public Result<PageData<CateDTO>> page( @RequestParam Map<String, Object> params){
//        PageData<CateDTO> page = cateService.page(params);
//
//        return new Result<PageData<CateDTO>>().ok(page);
//    }


    @GetMapping("list")
    @Operation(summary = "列表")
    @RequiresPermissions("yckj:cate:list")
    public Result<List<CateDTO>> list() {

        List<CateDTO> list = cateService.getList(new HashMap<>(1));

        return new Result<List<CateDTO>>().ok(list);
    }


    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("yckj:cate:info")
    public Result<CateDTO> get(@PathVariable("id") Long id) {
        CateDTO data = cateService.get(id);

        return new Result<CateDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    @RequiresPermissions("yckj:cate:save")
    public Result save(@RequestBody CateDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        dto.setCreateTime(new Date());
        dto.setUpdateTime(new Date());
        dto.setCreator(SecurityUser.getUser().getUsername());

        cateService.save(dto);

        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    @RequiresPermissions("yckj:cate:update")
    public Result update(@RequestBody CateDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        dto.setUpdateTime(new Date());
        dto.setUpdater(SecurityUser.getUser().getUsername());

        cateService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("yckj:cate:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        cateService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @Operation(summary = "导出")
    @LogOperation("导出")
    @RequiresPermissions("yckj:cate:export")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<CateDTO> list = cateService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "项目分类表", list, CateExcel.class);
    }

}